package com.sihai.ehr.web.action.salary;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.core.beans.Property;
import com.sihai.ehr.model.biz.Attendance;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.SalaryAttendanceService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

/**
 * author: wxn
 * since: 13-1-22 Time: 下午2:43
 * 底薪管理Action
 */
@Results({
        @Result(name = "main", location = "salary_main.jsp"),
        @Result(name = "list", location = "base_salary_list.jsp")
})
public class BaseSalaryAction extends EHRAction{
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private SalaryAttendanceService salaryAttendanceService;

    private List<Organization> organizations;

    private LimitedList<Attendance> attendances;

    private boolean showYearMonth;
    private int startYear;
    private int endYear;
    private int year;
    private int month;

    @Action("/salary/base_salary_main")
    @Override
    public String execute() throws Exception {
        listAction = "/salary/base_salary_list.xhtml";
        listTitle = "底薪管理";

        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        showChildOrg = true;
        showYearMonth = true;
        try{
            startYear = NumberUtils.createInteger(PropertiesLoaderUtils.loadAllProperties("jdbc.properties").getProperty("attendance_start_year", "2001"));
        }catch (Exception e){
            startYear = 2001;
        }
        setCurrentYearAndMonth();
        return "main";
    }

    @Action("/salary/base_salary_list")
    public String list()throws Exception{
        Long orgId = NumberUtils.createLong(getParameter("selectionOrgId"));
        Long dutyId = NumberUtils.createLong(getParameter("selectionDutyId"));
        Long searchOrgId = (orgId == null || orgId <= 0 ? organizationId : orgId);
        if(year <= 0 || month <= 0){
            setCurrentYearAndMonth();
        }
        attendances = salaryAttendanceService.find(year, month, searchOrgId, dutyId, createLimit(), createSorter());
        paging(attendances);
        setPageUrl("salary/base_salary_list.xhtml");
        return "list";
    }

    private void setCurrentYearAndMonth(){
        endYear = Calendar.getInstance().get(Calendar.YEAR);
        year = endYear;
        month = Calendar.getInstance().get(Calendar.MONTH);
        if(month == 0){
            month = 12;
            year = year-1;
        }
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public LimitedList<Attendance> getAttendances() {
        return attendances;
    }

    public int getStartYear() {
        return startYear;
    }

    public int getEndYear() {
        return endYear;
    }

    public boolean isShowYearMonth() {
        return showYearMonth;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }
}
